package com.google.samples.apps.databinging.recycler

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.google.samples.apps.databinging.R
import com.google.samples.apps.databinging.binding.Book
import com.google.samples.apps.databinging.databinding.LayoutItemBookBinding

/**
 * Created by Administrator on 2021/7/10 0010.
 * Describe:
 */
class BookAdapter(var list: ArrayList<Book>) : RecyclerView.Adapter<BookAdapter.BookVH>() {


    class BookVH(var binding: LayoutItemBookBinding) : RecyclerView.ViewHolder(binding.root) {
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookVH {
        return BookVH(
            DataBindingUtil.inflate<LayoutItemBookBinding>(
                LayoutInflater.from(parent.context),
                R.layout.layout_item_book,
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: BookVH, position: Int) {
        holder.binding.book = list[position]
    }

    override fun getItemCount(): Int {
        return list.size
    }

}